﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace huaguwang.hgmanage.book
{
    public partial class bookinfo_E : System.Web.UI.Page
    {
        D_T_Category_Exp _catedal = new D_T_Category_Exp();
        M_T_EBook _bmodel = new M_T_EBook();
        M_T_RealBook _rmodel = new M_T_RealBook();
        D_T_EBook_Exp _edal = new D_T_EBook_Exp();
        D_T_RealBook_Exp _rdal = new D_T_RealBook_Exp();
        private readonly D_T_ExperInfot_Exp m_expert = new D_T_ExperInfot_Exp();
        private void SetButton(int i, Button but)
        {
            switch(i)
            {
                case 1:
                    but.CausesValidation = false;
                    break;
                default:
                    break;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                this.WebUserToolBar1.MainInit(
                      UserConst.Exp_GetUser,
                      UserConst.Exp_GetButtonList,
                      SetButton,
                     UserConst.Exp_ToolBarErr
                  );
                if(!IsPostBack)
                {
                    //绑定类别
                    BindCateDrop();
                    //绑定专家
                    BindExpert();
                    var id = Request.Params["ID"].Exp_IntTryParse();
                    ViewState.Add("id", id);
                    if(id > 0)
                    {
                        var model = _edal.GetModel(id);//获得电子书实体
                        if(model == null)
                        {
                            this.Exp_PageSkyAlterReplace("bookinfo.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                        }
                        else
                        {
                            BindData(model);
                        }
                    }
                }
            });
        }

        /// <summary>
        /// 绑定下拉列表框
        /// </summary>
        private void BindCateDrop()
        {
            AdminPublic.Try(() =>
            {
                this.HGdropCate.Exp_BindClassTree();
            });
        }

        /// <summary>
        /// 绑定专家
        /// </summary>
        private void BindExpert()
        {
            AdminPublic.Try(() =>
            {
                var expertlist = m_expert.DropDownListDatData();
                this.DropDownList1.DataValueField = "ID";
                this.DropDownList1.DataTextField = "Name";
                this.DropDownList1.DataSource = expertlist;
                this.DropDownList1.DataBind();
            });
        }

        /// <summary>
        /// 修改绑定数据
        /// </summary>
        private void BindData(M_T_EBook model)
        {
            this.HGdropCate.SelectedValue = model.CategoryID.Exp_Trim();
            this.DropDownList1.SelectedValue = model.ExperInfotID.Exp_Trim();
            this.txtBookUrl.Text = model.URL.Exp_Trim();
            this.txtTitle.Text = model.Title.Exp_Trim();
            //图片显示
            FLWUpLoad1.SetIMG(model.Pic);
            this.txtGrade.Text = model.Grade.Exp_Trim();
            this.txtGold.Text = model.Gold.Exp_Trim();
            this.txtSource.Text = model.Source.Exp_Trim();
            //置顶为1，不置顶为0
            this.rdoListTopis.Text = model.Topis.Exp_Trim();
            //推荐为1，不推荐为0
            this.rblRem.Text = model.RemSign ? "1" : "0";
            this.txtSEOtitle.Text = model.WebTitle.Exp_Trim();
            this.txtSEOkey.Text = model.WebKey.Exp_Trim();
            this.txtSEOdec.Text = model.WebSummary.Exp_Trim();
            this.txtContent.Value = model.Explain.Exp_Trim();
            //有实体书为1，没有为0
            this.rdoList.Text = model.HasBook ? "1" : "0";
            if(this.rdoList.Text == "1") //绑定实体书信息
            {
                this.panRealBook.Visible = true;
                var RealBookModel = _rdal.GetList("EbookID = " + model.id, null);
                foreach(var r in RealBookModel)
                {
                    this.txtRealBookName.Text = r.Title.Exp_Trim();
                    this.txtRealISBN.Text = r.ISBN.Exp_Trim();
                    this.txtRealPublish.Text = r.Publish.Exp_Trim();
                    this.txtRealAuthor.Text = r.Author.Exp_Trim();
                    this.txtRealPrice.Text = r.Price.Exp_Trim();
                    //实体书封面
                    FLWUpLoad2.SetIMG(r.Pic);
                    this.txtRealGold.Text = r.Gold.Exp_Trim();
                    this.txtRealSellCount.Text = r.SellNum.Exp_Trim();
                    this.txtStockCount.Text = r.StockNum.Exp_Trim();
                    this.txtRealContent.Value = r.Explain.Exp_Trim();
                    break;
                }
            }
        }

        /// <summary>
        /// 电子书实体
        /// </summary>
        /// <returns></returns>
        private M_T_EBook ControlsToModel()
        {
            _bmodel.id = ViewState["id"].Exp_IntTryParse();
            _bmodel.Title = this.txtTitle.Text.Exp_Trim();
            _bmodel.Source = this.txtSource.Text.Exp_Trim();
            _bmodel.CategoryID = this.HGdropCate.SelectedValue.Exp_IntTryParse();
            _bmodel.Type = "pdf";
            _bmodel.URL = this.txtBookUrl.Text.Exp_Trim();
            _bmodel.Size = 1024;
            _bmodel.Explain = this.txtContent.Value.Exp_Trim();
            _bmodel.Gold = this.txtGold.Text.Exp_DecimalTryParse();
            _bmodel.Grade = this.txtGrade.Text.Exp_IntTryParse();
            //是否有实体书
            _bmodel.HasBook = this.rdoList.Text == "0" ? false : true;
            _bmodel.WebKey = this.txtSEOkey.Text != "" ? this.txtSEOkey.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
            _bmodel.WebSummary = this.txtSEOdec.Text != "" ? this.txtSEOdec.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
            _bmodel.WebTitle = this.txtSEOtitle.Text != "" ? this.txtSEOtitle.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
            _bmodel.ColorKey = this.txtColorKey.Text != "" ? this.txtColorKey.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
            _bmodel.ColorTitle = this.txtColorTitle.Text != "" ? this.txtColorTitle.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
            _bmodel.InteriorKey = this.txtInKey.Text != "" ? this.txtInKey.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
            _bmodel.AddDate = DateTime.Now;
            //置顶
            _bmodel.Topis = this.rdoListTopis.Text.Exp_IntTryParse();
            //推荐
            _bmodel.RemSign = this.rblRem.Text == "0" ? false : true;
            //获取操作用户ID
            _bmodel.UserID = UserConst.Exp_GetUser(this).id;
            _bmodel.ExperInfotID = this.DropDownList1.SelectedValue.Exp_IntTryParse();
            return _bmodel;
        }

        /// <summary>
        /// 实体书实体
        /// </summary>
        /// <returns></returns>
        private M_T_RealBook GetRealbookModel()
        {
            _rmodel.EbookID = ViewState["id"].Exp_IntTryParse();
            _rmodel.Title = this.txtRealBookName.Text.Exp_Trim();
            _rmodel.Author = this.txtRealAuthor.Text.Exp_Trim();
            _rmodel.ISBN = this.txtRealISBN.Text.Exp_Trim();
            _rmodel.Explain = this.txtRealContent.Value.Exp_Trim();
            _rmodel.Gold = this.txtRealGold.Text.Exp_DecimalTryParse();
            _rmodel.Grade = 0;
            _rmodel.WebKey = string.Empty;
            _rmodel.WebSummary = string.Empty;
            _rmodel.WebTitle = string.Empty;
            _rmodel.ColorKey = string.Empty;
            _rmodel.ColorTitle = string.Empty;
            _rmodel.InnerKey = string.Empty;
            _rmodel.SellNum = this.txtRealSellCount.Text.Exp_IntTryParse();
            _rmodel.StockNum = this.txtStockCount.Text.Exp_IntTryParse();
            _rmodel.Publish = this.txtRealPublish.Text.Exp_Trim();
            return _rmodel;
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                //注意,统一,先获取实体,然后根据实体进行各种判断
                var model = ControlsToModel();//电子书实体
                var realModel = GetRealbookModel();//实体书实体
                var LogType = Log_Type.add;
                var iRef = 0;
                if (model.id <= 0)//新增
                {
                    var fileName = this.FLWUpLoad1.UpLoad();
                    if (!fileName)
                    {
                        model.Pic = "";
                    }
                    else
                    {
                        model.Pic = FLWUpLoad1.FilePath;
                    }
                    if (this.rdoList.Text == "1")
                    {
                        var fileName1 = this.FLWUpLoad2.UpLoad();
                        if (!fileName)
                        {
                            realModel.Pic = "";
                        }
                        else
                        {
                            realModel.Pic = FLWUpLoad2.FilePath;
                        }
                    }
                    iRef = _edal.AddExp(model, this.rdoList.Text == "1", realModel);
                }
                else
                {
                    //修改的专用判断
                    LogType = Log_Type.update;
                    var Temp = _edal.ExistsFlag(model.id);
                    if (!Temp)
                    {
                        this.Exp_PageAlter("资料不存在,可能被其他人删除");
                        return;
                    }
                    //如果要保存图片,请注意此处的用法
                    var PicTemp = _edal.GetModelFlag(model.id);
                    var fileName = this.FLWUpLoad1.UpLoad();
                    if (!fileName)//上传失败
                    {
                        model.Pic = PicTemp.Pic;
                    }
                    else
                    {
                        model.Pic = FLWUpLoad1.FilePath;
                    }
                    //如果要保存图片,请注意此处的用法
                    var RealTemp = _rdal.GetModelFlagExp(realModel.EbookID);
                    var fileName1 = this.FLWUpLoad2.UpLoad();
                    if (this.rdoList.Text == "1")
                    {
                        if (!fileName1)
                        {
                            realModel.Pic = RealTemp.Pic;
                        }
                        else
                        {
                            realModel.Pic = FLWUpLoad2.FilePath;
                        }
                    }
                    else if (realModel.EbookID>0)//删除实体书
                    {
                        _rdal.UpdateFlagExp(realModel.EbookID);
                        realModel = null;
                    }
                    iRef = _edal.UpdateExp(model, this.rdoList.Text == "1", realModel);
                }
                EditSaveMsg(iRef, LogType);
            });
        }


        /// <summary>
        /// 操作完成后,处理各种提示用
        /// </summary>
        /// <param name="iRef"></param>
        /// <param name="LogType"></param>
        public void EditSaveMsg(int iRef, Log_Type LogType)
        {
            if(iRef > 0)
            {
                this.Exp_PageWriteLog(LogType, "书籍信息维护", iRef);
                this.Exp_PageSkyAlterReplace("bookinfo.aspx", WebUserToolBar1.EditSKey, "操作成功");
            }
            else
            {
                switch(iRef)
                {
                    default:
                        this.Exp_PageAlter("操作失败");
                        break;
                }
            }
        }

        /// <summary>
        /// 返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = this.WebUserToolBar1.EditSKey;
                Response.Redirect("bookinfo.aspx?" + sKey);
            });
        }

        protected void rdoList_SelectedIndexChanged(object sender, EventArgs e)
        {
            panRealBook.Visible = !panRealBook.Visible;
        }
    }
}
